package com.xhx.accounting;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class setting extends AppCompatActivity {
    private View feedback,share,setBudget;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        feedback = findViewById(R.id.feedback);
        share = findViewById(R.id.share);
        setBudget = findViewById(R.id.set_budget);

        setBudget.setOnClickListener(view -> {
            final EditText editText = new EditText(setting.this);
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
            AlertDialog.Builder inputDialog =
                    new AlertDialog.Builder(setting.this);
            inputDialog.setTitle("请输入预算").setView(editText);
            inputDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            inputDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            int money = Integer.parseInt(editText.getText().toString());
                            DBDao db = new DBDao(setting.this);
                            db.changeBudget(money);
                        }
                    }).show();
        });

        feedback.setOnClickListener(view -> {
            Uri uri = Uri.parse("mailto:");
            String[] email = {"catherinesurvive@163.com"};
            Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
            intent.putExtra(Intent.EXTRA_EMAIL, email);
            intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback:刀刃记账 问题反馈"); // 主题
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent.createChooser(intent, "请选择邮件应用"));
        });
        share.setOnClickListener(view -> {
            shareText(setting.this, "“刀刃记账”下载地址：https://gitee.com/asteriod_0217");
        });
    }

    public static void shareText(Context context, String extraText) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "应用分享");
        intent.putExtra(Intent.EXTRA_TEXT, extraText);//extraText为文本的内容
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//为Activity新建一个任务栈
        context.startActivity(
                Intent.createChooser(intent, "请选择要分享到的应用"));//R.string.action_share同样是标题
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.arrow_left:
                finish();
                break;
        }
    }
}